package com.itheima.controller;

import com.itheima.advice.RecordLogAnno;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result pageEmp(EmpQueryParam empQueryParam) {
        PageResult<Emp> pageResult = empService.pageEmp(empQueryParam);
        log.info("执行员工分页查询");
        return Result.success(pageResult);
    }

    @PostMapping
    @RecordLogAnno
    public Result save(@RequestBody Emp emp) throws Exception {
        empService.saveEmpAndExpr(emp);
        log.info("添加员工信息");
        return Result.success();
    }

    @DeleteMapping
    @RecordLogAnno
    public Result deleteById(@RequestParam List<Integer> ids) {
        empService.deleteEmpAndExprById(ids);
        log.info("删除员工信息 id: " +ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Emp emp = empService.getEmpAndExprById(id);
        log.info("员工数据回显");
        return Result.success(emp);
    }

    @PutMapping
    @RecordLogAnno
    public Result update(@RequestBody Emp emp) {
        empService.updateEmpAndExpr(emp);
        log.info("修改员工信息");
        return Result.success();
    }

    @GetMapping("/list")
    public Result getAllEmp() {
        List<Emp> empList = empService.list();
        log.info("查询全部员工信息");
        return Result.success(empList);
    }
}
